<!DOCTYPE html>
<html lang="zh-Hans">
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="min peng" />



<meta name="description" content="SpringBoot WebSocket使用">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringBoot WebSocket使用">
<meta property="og:url" content="https://minpeng.github.io/2019/08/12/SpringBoot-WebSocket使用/index.html">
<meta property="og:site_name" content="Pengm">
<meta property="og:description" content="SpringBoot WebSocket使用">
<meta property="og:updated_time" content="2019-08-12T12:59:29.465Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="SpringBoot WebSocket使用">
<meta name="twitter:description" content="SpringBoot WebSocket使用">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="Pengm" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">





    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>SpringBoot WebSocket使用 | Pengm</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: undefined,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script> yiliaConfig.jquery_ui = [false]; </script>



    <script> yiliaConfig.rootUrl = "\/";</script>





    <script>
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "//hm.baidu.com/hm.js?b3aa41a56e6fde6d00c616ac89493f29";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
        })();
    </script>


</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">min peng</a></h1>
        </hgroup>

        
        <p class="header-subtitle">Pengm blog</p>
        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="false" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/archives/">所有文章</a></li>
                        
                            <li><a href="/tags/">标签云</a></li>
                        
                            <li><a href="/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:pengmin3@163.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="https://github.com/minpeng" title="GitHub"></a>
                            
                                <a class="fa RSS" href="/atom.xml" title="RSS"></a>
                            
                                <a class="fa CSDN" href="http://blog.csdn.net/u010742049" title="CSDN"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/HBase/">HBase</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/">Linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Mysql/">Mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SpringBoot/">SpringBoot</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/angular/">angular</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/">git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/memcahce/">memcahce</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/quill/">quill</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/scala/">scala</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/scrapy/">scrapy</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/shiro/">shiro</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spark/">spark</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spring/">spring</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/stream/">stream</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/yelee/">yelee</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/系统架构/">系统架构</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/读书笔记/">读书笔记</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/随笔/">随笔</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="https://hexo.io">Hexo</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://pages.github.com/">GitHub</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://moxfive.xyz/">MOxFIVE</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">一个爱运动不是太宅的技术大男孩</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">min peng</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">min peng</a></h1>
            </hgroup>
            
            <p class="header-subtitle">Pengm blog</p>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/archives/">所有文章</a></li>
                
                    <li><a href="/tags/">标签云</a></li>
                
                    <li><a href="/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:pengmin3@163.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="https://github.com/minpeng" title="GitHub"></a>
                            
                                <a class="fa RSS" target="_blank" href="/atom.xml" title="RSS"></a>
                            
                                <a class="fa CSDN" target="_blank" href="http://blog.csdn.net/u010742049" title="CSDN"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-SpringBoot-WebSocket使用" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2019/08/12/SpringBoot-WebSocket使用/" class="article-date">
      <time datetime="2019-08-12T12:57:24.770Z" itemprop="datePublished">2019-08-12</time>
</a>


    </div>
  
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      SpringBoot WebSocket使用
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="/categories/SpringBoot/">SpringBoot</a>
    </div>


        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <contents>

<h2 id="spring-boot-webScoket使用"><a href="#spring-boot-webScoket使用" class="headerlink" title="spring boot webScoket使用"></a>spring boot webScoket使用</h2><h3 id="1-pom文件配置"><a href="#1-pom文件配置" class="headerlink" title="1.pom文件配置"></a>1.pom文件配置</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">&lt;dependency&gt;</div><div class="line">       &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</div><div class="line">       &lt;artifactId&gt;spring-boot-starter-websocket&lt;/artifactId&gt;</div><div class="line">&lt;/dependency&gt;</div></pre></td></tr></table></figure>
<h3 id="2-webSocket-bean配置"><a href="#2-webSocket-bean配置" class="headerlink" title="2.webSocket bean配置"></a>2.webSocket bean配置</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">@Configuration</div><div class="line">@EnableWebSocket</div><div class="line">public class CafeWebSocketConfig implements WebSocketConfigurer &#123;</div><div class="line">    @Override</div><div class="line">    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) &#123;</div><div class="line">        //websocket 切点url</div><div class="line">        registry.addHandler(CafeWebSocketHandler(), &quot;/cafeWebSocket&quot;)</div><div class="line">                .addInterceptors(CafeHandshakeInterceptor());</div><div class="line">        //如果有跨域的情况,需要加入</div><div class="line">        setAllowedOrigins(&quot;*&quot;);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    //注入处理逻辑</div><div class="line">    @Bean</div><div class="line">    public WebSocketHandler CafeWebSocketHandler() &#123;</div><div class="line">        return new CafeWebSocketHandler();</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    //注入拦截器</div><div class="line">    @Bean</div><div class="line">    public HandshakeInterceptor CafeHandshakeInterceptor() &#123;</div><div class="line">        return new CafeHandshakeInterceptor();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="3-webSocket拦截器配置"><a href="#3-webSocket拦截器配置" class="headerlink" title="3.webSocket拦截器配置"></a>3.webSocket拦截器配置</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">//实现HandshakeInterceptor接口</div><div class="line">public class CafeHandshakeInterceptor implements HandshakeInterceptor &#123;</div><div class="line">    //权限url</div><div class="line">    private static final String PERMISSION=&quot;/webSocketUrl&quot;;</div><div class="line">    </div><div class="line">    //握手之前调用</div><div class="line">    //权限判断</div><div class="line">    @Override</div><div class="line">    public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Map&lt;String, Object&gt; map) throws Exception &#123;</div><div class="line">        ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) serverHttpRequest;</div><div class="line"></div><div class="line">        //获取用户</div><div class="line">        boolean user = ShiroKit.isUser();</div><div class="line">        if(!user)&#123;</div><div class="line">            return false;</div><div class="line">        &#125;</div><div class="line">        //获取权限</div><div class="line">        //判断是否有webSocket权限</div><div class="line">        boolean permisson = ShiroKit.hasPermission(PERMISSION);</div><div class="line">        if(!permisson)&#123;</div><div class="line">            return false;</div><div class="line">        &#125;</div><div class="line">        //把账号信息放到webSocketSession中</div><div class="line">        map.put(WEBSOCKET_ACCOUNT,ShiroKit.getUser().getAccount());</div><div class="line">        return true;</div><div class="line">    &#125;</div><div class="line">    //握手之后调用</div><div class="line">    @Override</div><div class="line">    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) &#123;</div><div class="line">        logger.info(ShiroKit.getUser().getAccount()+&quot;,成功连接cafeWebSocket...&quot;);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="4-webSocket消息处理器"><a href="#4-webSocket消息处理器" class="headerlink" title="4.webSocket消息处理器"></a>4.webSocket消息处理器</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">@Component</div><div class="line">public class CafeWebSocketHandler extends TextWebSocketHandler &#123;</div><div class="line">    //全局session,可以存所有用户的session,但是不支持分布式</div><div class="line">    private final static CopyOnWriteArraySet&lt;WebSocketSession&gt; sessions = new CopyOnWriteArraySet&lt;&gt;();</div><div class="line">   </div><div class="line">   //在WebSocket协商成功后调用，并且打开WebSocket连接准备使用</div><div class="line">    @Override</div><div class="line">    public void afterConnectionEstablished(WebSocketSession session) throws Exception &#123;</div><div class="line">        super.afterConnectionEstablished(session);</div><div class="line">        sessions.add(session);</div><div class="line"></div><div class="line">        logger.info(&quot;【WebSocket】有新的连接, sessionId:&#123;&#125;,account:&#123;&#125;,总数:&#123;&#125;&quot;,</div><div class="line">                session.getId(), (String) session.getAttributes().get(WEBSOCKET_ACCOUNT), sessions.size());</div><div class="line"></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    //关闭webSocket连接</div><div class="line">    @Override</div><div class="line">    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception &#123;</div><div class="line">        super.afterConnectionClosed(session, status);</div><div class="line">        sessions.remove(session);</div><div class="line">        logger.info(&quot;【WebSocket】连接断开, sessionId:&#123;&#125;,userName:&#123;&#125;,总数:&#123;&#125;&quot;,</div><div class="line">                session.getId(), (String) session.getAttributes().get(WEBSOCKET_ACCOUNT), sessions.size());</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    //发送消息</div><div class="line">    @Override</div><div class="line">    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception &#123;</div><div class="line">        super.handleTextMessage(session, message);</div><div class="line">        session.sendMessage(message);</div><div class="line">    &#125; </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="5-前端配置"><a href="#5-前端配置" class="headerlink" title="5.前端配置"></a>5.前端配置</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">getWebSocket = function () &#123;</div><div class="line">    var webSocket = null;</div><div class="line">    var heartBeatMessage=&quot;HeartBeat&quot;;</div><div class="line">    if(&apos;WebSocket&apos; in window)&#123;</div><div class="line">        if(window.location.protocol===&quot;https:&quot;)&#123;</div><div class="line">            webSocket = new WebSocket(&apos;wss://&apos;+window.location.host+&apos;/webSocket&apos;);</div><div class="line">        &#125;else&#123;</div><div class="line">            webSocket = new WebSocket(&apos;ws://&apos;+window.location.host+&apos;/webSocket&apos;);</div><div class="line">        &#125;</div><div class="line"></div><div class="line">    &#125;else&#123;</div><div class="line">        console.log(&quot;该浏览器不支持WebSocket!!!&quot;);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    webSocket.onopen = function (event) &#123;</div><div class="line">        heartCheck.start();</div><div class="line">        console.log(&quot;建立连接&quot;);</div><div class="line">      </div><div class="line"></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    webSocket.onclose = function (event) &#123;</div><div class="line">        console.log(&quot;断开连接&quot;);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    webSocket.onmessage = function (event) &#123;</div><div class="line">        heartCheck.reset();</div><div class="line">        if(event.data==&apos;&apos;|| event.data==undefined ||</div><div class="line">            event.data==heartBeatMessage)&#123;</div><div class="line">            return;</div><div class="line">        &#125;</div><div class="line">        //获取数据</div><div class="line">        var jsObject = JSON.parse(event.data);</div><div class="line">     </div><div class="line">    &#125;</div><div class="line"></div><div class="line">    webSocket.onerror = function (event) &#123;</div><div class="line">        console.log(&quot;websocket通信发生错误!!!&quot;);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    window.onbeforeunload = function (event) &#123;</div><div class="line">        webSocket.close();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    //心跳检测</div><div class="line">    var heartCheck=&#123;</div><div class="line">        timeout: 58*1000,//58s</div><div class="line">        timeoutObj: null,</div><div class="line">        reset: function()&#123;</div><div class="line">            clearTimeout(this.timeoutObj);</div><div class="line">            this.start();</div><div class="line">        &#125;,</div><div class="line">        start: function()&#123;</div><div class="line">            this.timeoutObj = setTimeout(function()&#123;</div><div class="line">                webSocket.send(&quot;HeartBeat&quot;);</div><div class="line">            &#125;, this.timeout)</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;;</div></pre></td></tr></table></figure></contents>
      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/2019/08/12/SpringBoot-WebSocket使用/">SpringBoot WebSocket使用</a></p>
        <p><span>文章作者:</span><a href="/" title="回到主页">min peng</a></p>
        <p><span>发布时间:</span>2019-08-12, 20:57:24</p>
        <p><span>最后更新:</span>2019-08-12, 20:59:29</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/2019/08/12/SpringBoot-WebSocket使用/" title="SpringBoot WebSocket使用">https://minpeng.github.io/2019/08/12/SpringBoot-WebSocket使用/</a>
            <span class="copy-path" data-clipboard-text="原文: https://minpeng.github.io/2019/08/12/SpringBoot-WebSocket使用/　　作者: min peng" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/2019/08/12/quill富文本编辑器自定义上传图片/">
                    quill富文本编辑器自定义上传图片
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/2019/08/12/SpringBoot事务解析/">
                    SpringBoot事务解析
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#spring-boot-webScoket使用"><span class="toc-number">1.</span> <span class="toc-text">spring boot webScoket使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-pom文件配置"><span class="toc-number">1.1.</span> <span class="toc-text">1.pom文件配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-webSocket-bean配置"><span class="toc-number">1.2.</span> <span class="toc-text">2.webSocket bean配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-webSocket拦截器配置"><span class="toc-number">1.3.</span> <span class="toc-text">3.webSocket拦截器配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-webSocket消息处理器"><span class="toc-number">1.4.</span> <span class="toc-text">4.webSocket消息处理器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-前端配置"><span class="toc-number">1.5.</span> <span class="toc-text">5.前端配置</span></a></li></ol></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"SpringBoot WebSocket使用　| Pengm　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    
      <!-- 来必力City版安装代码 -->
	<div id="lv-container" data-id="city"  data-uid="MTAyMC8yODg4MS81NDUx">
		<script type="text/javascript">
	   (function(d, s) {
		   var j, e = d.getElementsByTagName(s)[0];

		   if (typeof LivereTower === 'function') { return; }

		   j = d.createElement(s);
		   j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		   j.async = true;

		   e.parentNode.insertBefore(j, e);
	   })(document, 'script');
		</script>
	<noscript> 为正常使用来必力评论功能请激活JavaScript</noscript>
	</div>
	<!-- City版安装代码已完成 -->

    




    <div class="scroll" id="post-nav-button">
        
            <a href="/2019/08/12/quill富文本编辑器自定义上传图片/" title="上一篇: quill富文本编辑器自定义上传图片">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/2019/08/12/SpringBoot事务解析/" title="下一篇: SpringBoot事务解析">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/2020/03/21/java8Stream常用/">java8Stream常用</a></li><li class="post-list-item"><a class="post-list-link" href="/2019/11/05/git使用指南/">git使用指南</a></li><li class="post-list-item"><a class="post-list-link" href="/2019/08/24/SpringBoot自定义配置文件区分环境/">SpringBoot自定义配置文件区分环境</a></li><li class="post-list-item"><a class="post-list-link" href="/2019/08/12/quill富文本编辑器自定义上传图片/">quill富文本编辑器自定义上传图片</a></li><li class="post-list-item"><a class="post-list-link" href="/2019/08/12/SpringBoot-WebSocket使用/">SpringBoot WebSocket使用</a></li><li class="post-list-item"><a class="post-list-link" href="/2019/08/12/SpringBoot事务解析/">SpringBoot事务解析</a></li><li class="post-list-item"><a class="post-list-link" href="/2018/07/22/记两年工作总结/">记两年工作总结</a></li><li class="post-list-item"><a class="post-list-link" href="/2018/06/22/读书笔记-疯狂Java/">读书笔记-疯狂Java</a></li><li class="post-list-item"><a class="post-list-link" href="/2018/05/22/读书笔记-大型网站技术架构/">读书笔记-大型网站技术架构</a></li><li class="post-list-item"><a class="post-list-link" href="/2018/05/22/shiro权限控制/">shiro权限控制</a></li><li class="post-list-item"><a class="post-list-link" href="/2018/04/22/jdbc原生api批量插入数据库/">jdbc原生api批量插入数据库</a></li><li class="post-list-item"><a class="post-list-link" href="/2018/03/22/序列化拷贝对象/">序列化拷贝对象</a></li><li class="post-list-item"><a class="post-list-link" href="/2018/01/22/spring-memcache缓存注解配置/">spring+memcache缓存注解配置</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/16/java数字转换/">java数字转换</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/16/一年工作之感悟/">一年工作之感悟</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/09/Spring框架简单异步发送邮件/">Spring框架简单异步发送邮件</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/07/09/Mysql优化/">Mysql优化</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/05/29/linux/">Linux 生疏命令</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/04/03/Scrapy-xinmin/">Scrapy爬取新民网</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/03/03/java-demo/">java代码审查</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/12/23/StudySpark/">学习scala和spark的一些内容</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/12/17/我的第一篇博客/">我的第一篇博客：HBase</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/12/16/hello-world/">Hello World</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2016-2020 min peng
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>

    </div>
    
<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 5;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
            
             title: "a.article-title, .article-more-link a", 
            
             tags: ".article-tag a", 
             categories: ".article-category a, a.tag-list-link", 
             articleNav: "#article-nav a, #post-nav-button a", 
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>